/*
 * B.c
 *
 *  Created on: 2021年11月24日
 *      Author: l
 */

/*
 * PPS_IN   PH3 GPS
 * IRGB		PD9 USART3_RX
 */
#include"B.h"
#include "tim.h"

struct IRGB_Time_Type IRGB_Time;
struct IRGB_Time_Type IRGB_Time_count[10];
int IRGB_Time_count_num = 0;
int IRGB_Rising_Time[100];
IRGB_Flag =0;

IRGB_Rising_Num = 0;


IRGB_Rising_Time_Start = 0;
IRGB_Rising_Time_End = 0;



void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	IRGB_Rising_Time_Start = __HAL_TIM_GET_COUNTER(&htim6);
	if(GPIO_Pin == GPIO_PIN_9)
	{

		if(IRGB_Rising_Num == 99)
		{
			IRGB_Rising_Num = 0;
		}

	}
}
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
	IRGB_Rising_Time_End = __HAL_TIM_GET_COUNTER(&htim6);

	if(GPIO_Pin == GPIO_PIN_9)
	{

		IRGB_Rising_Time[IRGB_Rising_Num] = IRGB_Rising_Time_End - IRGB_Rising_Time_Start;
		//printf("IRGB_Rising_Time[%d] = %d\r\n",IRGB_Rising_Num,IRGB_Rising_Time[IRGB_Rising_Num]);
		if(IRGB_Rising_Num==1)
		{
//			printf("IRGB_Rising_Time[%d] = %d\r\n",0,IRGB_Rising_Time[0]);
//			printf("IRGB_Rising_Time[%d] = %d\r\n",1,IRGB_Rising_Time[1]);
			//只有满足 0,1 都是80 收集数据开始
			if(checkP(IRGB_Rising_Time[0]) && checkP(IRGB_Rising_Time[1]))
			{

			}
			else
			{
				IRGB_Rising_Time[0] = IRGB_Rising_Time[1]; //处理20 80 80 50的情况
				IRGB_Rising_Num = 0;
			}
		}
		if(IRGB_Rising_Num == 42)
		{
//			printf("IRGB_Rising_Time[%d] = %d\r\n",0,IRGB_Rising_Time[0]);//25ms
//			printf("IRGB_Rising_Time[%d] = %d\r\n",1,IRGB_Rising_Time[1]);
//			for(int i=0;i++;i<42)
//			{
//				printf("IRGB_Rising_Time[%d] = %d\r\n",i,IRGB_Rising_Time[i]);
//			}

			deal_B_Time(IRGB_Rising_Time,IRGB_Rising_Num);//0ms

		}
		IRGB_Rising_Num++;
	}
}
/*
 * 新码元开始：P码,间隔码：0码
 * 0码元 2-5位 秒  7-9位十秒  0位P码表示1个新帧开始，1位P码表示新码元开始，6位为0码即间隔码
 * 1码元 1-4位 分  6-9位十分  0位P码表示新码元开始，5位为0码即间隔码
 * 2码元 1-4位 时  6-9位十时  0位P码表示新码元开始，5位为0码即间隔码
 * 3码元 1-4位 天  6-9位十天  0位P码表示新码元开始，5位为0码即间隔码
 * 4码元 1-2位 年
 *
 *
 * 8码元 1-9位与9码元1-8位一起为秒数 9码元第9位为0码，标志1帧数据结束
 * 每个码元计算都是左边为0次方开始
 */


void deal_B_Time(int *IRGB_Rising_Time,int num)
{
//	printf("IRGB_Rising_Time[%d] = %d\r\n",1,*(IRGB_Rising_Time+1));
//	for(int i=0;i++;i<num)
//	{
//		printf("*(IRGB_Rising_Time+%d)= %d\r\n",i,*(IRGB_Rising_Time+i));
//	}

	IRGB_Time.sec = my2_10(IRGB_Rising_Time+2,4)+10*my2_10(IRGB_Rising_Time+7,3);

	IRGB_Time.min = my2_10(IRGB_Rising_Time+11,4)+10*my2_10(IRGB_Rising_Time+16,4);
	IRGB_Time.hour = my2_10(IRGB_Rising_Time+21,4)+10*my2_10(IRGB_Rising_Time+26,4);
	IRGB_Time.days = my2_10(IRGB_Rising_Time+31,4)+10*my2_10(IRGB_Rising_Time+36,4)+100*my2_10(IRGB_Rising_Time+41,2);
	IRGB_Time.year = 2021;
	int month_day[2]={0};
    Month_Day(IRGB_Time.year,IRGB_Time.days,month_day);
    IRGB_Time.month = month_day[0];
    IRGB_Time.day = month_day[1];
    IRGB_Flag=1;
//    IRGB_Time_count[IRGB_Time_count_num] = IRGB_Time;
//    IRGB_Time_count_num++;
//    if(IRGB_Time_count_num == 3)
//    {
//    	for(int i=0;i<IRGB_Time_count_num;i++)
//    	{
//    		printf("i= %d %d-%d-%d-%d-%d-%d \r\n",i,IRGB_Time_count[i].year,IRGB_Time_count[i].month,IRGB_Time_count[i].day,IRGB_Time_count[i].hour,IRGB_Time_count[i].min,IRGB_Time_count[i].sec);
//    	}
//    	IRGB_Time_count_num = 0;
//    }
//    printf("%d-%d-%d-%d-%d-%d \r\n",IRGB_Time.year,IRGB_Time.month,IRGB_Time.day,IRGB_Time.hour,IRGB_Time.min,IRGB_Time.sec);



}


int P_1_0(int high_time)//判断是0码、1码和P码
{
//	printf("high_time = %d\r\n",high_time);
	if(high_time == 20)
	{
		return 0;
	}
	if(high_time == 50)
	{
		return 1;
	}
	if(high_time == 80)
	{
		return 2;
	}
}

int my2_10(int *pData,int len)
{
    int data = 0;
    for(int i=0;i<len;i++)
    {
        if(check1(*(pData+i)))
        {

            data+=1<<i;
        }
    }
    return data;
}
